#pragma once
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <string>
#include <clocale>
#include <vector>
#include "room.h"

using namespace std;

class Building
{
	vector<room> v;
	string info;

public:

	Building()
	{
		info = "";
	}

	string GetInfo() {
		return info; }
	void SetInfo(string info){
		this->info = info; }


	void add(room b){
		v.push_back(b);
	}

	string SearchRoom(string type_of_room)
	{
		ostringstream oss;

		for (auto &item : v)
		{
			if (item.GetType_of_room() == type_of_room)
			{
				oss << item << endl;
			}
		}
		return oss.str();
	}

	string list(){
		ostringstream oss;

		for (int i = 0; i < v.size(); i++){
			oss << i << ".  " << v[i] << endl;
		}
		return oss.str();
	}
	void del(int i){

		v.erase(v.begin() + i);

	}

	void Delete(){

		int d = 0;
		cout << list() << endl;
		cout << "Ange en post att ta bort" << endl;
		cin >> d;
		del(d);
		cout << endl;
		cout << list() << endl;

	}

	void WriteToFile(string out){

		ofstream fout;
		fout.open(out);
		fout << "Building=" << this->info << endl;

		for (int i = 0; i < v.size(); i++)
		{
			fout << v[i] << endl;

		}
		fout.close();
	}
		
	void ReadFromFile(string in){

		string strRow, str;
		ifstream fin;
		fin.open(in);
		v.clear();
		while (std::getline(fin, strRow)){
			istringstream iss(strRow);
			getline(iss, str, '=');
			if (str.compare("info") == 0)
			{
				getline(iss, this->info);
			}
			else if (str.compare("room") == 0)
			{
				room a;
				iss >> a;
				if (a.GetArea() != -1){
					v.push_back(a);
				}

			}

			fin.close();
		}
	}

	void ReadFromFile()
	{
		ReadFromFile("Building.txt");
	}

	double Area(){

		double value = 0;

		for (int i = 0; i < v.size(); i++){
			value += v[i].GetArea();
		}

		cout << "arean av rummen =" << value << endl;
		return value;
	}

	string SearchWordInInfo(string word)
	{
		ostringstream oss;

		for (auto &item : GetInfo())
		{
			if (GetInfo().find(word) != string::npos)
			{
				oss << item << endl;
			}
		}
		return oss.str();
	}


	~Building()
	{
	}
};

